<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>商品详情</title>
    <link rel="stylesheet" href="../css/首页.css">
    <link rel="stylesheet" href="../css/商品详情.css">
    <script src="../js/cookie.js"></script>
    <script src="../js/jquery.js"></script>
    <script src="../js/jquery.min.js"></script>
    <script>
        $(function(){
            let id = location.search.split('=')[2];
            let phone = location.search.split('=')[1].split('&')[0];
            if(isNaN(id)){
                id=phone;
            }
            console.log(id);
            console.log(phone);
            $.ajax({
                type:"get",
                url:"../lib/getitem.php",
                data: {
                    id: id
                },
                dataType: "json",
                success:function(data){
                    let pic = JSON.parse(data.pic);
                    let pic1=JSON.parse(data.recompic);
                    let pic2=JSON.parse(data.intropic);
                    let pic3=JSON.parse(data.evapic);
                    console.log(data);
                    let temp1=`
                        <ul id="path-nav">
                            <li><a href="../html/首页.html">首页</a><em> > </em></li>
                            <li><a href="#">${data.type}</a><em> > </em></li>
                            <li class="active">${data.details}</li>
                        </ul>
                    `;
                    // console.log(temp1);
                    $('.temp1').append(temp1);
                    var img=document.querySelector('#pic'); 
                    // console.log(pic[0].src);
                    img.src=pic[0].src;
                    let temp2="";
                    pic.forEach((element,i) => {
                        temp2+=`
                            <li value="0"><img src="${pic[i].src}" alt=""></li>
                        `;
                    });
                    // console.log(temp2);
                    $('.temp2').append(temp2);
                    $("#product #Carousel .hd ul li").eq(0).css('border','2px solid #2d8701');
                    $("#product #Carousel .hd ul li img").on('mouseenter',
                        function(){
                            var url=this.src;
                            console.log(url);
                            img.src=url;
                            $("#product #Carousel .hd ul li").css('border','0');
                            $(this).parent().css('border','2px solid #2d8701');
                    }),
                    $("#product #Carousel .hd ul li img").on('mouseleave',
                        function(){
                            $("#product #Carousel .hd ul li").val("0");
                            $(this).parent().val("1");                                       
                            $("#product #Carousel .hd ul li").css('border','0');
                            $(this).parent().css('border','2px solid #2d8701');
                    });
                    let temp3=`
                        <div class="item-info" >
                            <h3>${data.details}
                                <span style="color: #2d8701;">${data.posting}</span>
                            </h3>
                            <p style="color: #777777;">${data.introduction}</p>
                            <div style="overflow: hidden" id="info">
                                <span style="color: #999">礼品卡&nbsp;</span><span>${data.dis}</span><span>%</span><br>
                                <span style="color: #999">可用&nbsp;</span><span>${data.giftcard}</span><span>元礼品卡&nbsp;&nbsp;</span>
                                <span>+&nbsp;￥</span><span>${data.giftprice}</span>
                            </div>
                            <div id="list" style="margin-top: 5px;">
                                <dl>
                                    <dt>价格：</dt>
                                    <dd>
                                        <span id="font">
                                            <em style="font-size: 65%;">￥</em>
                                            <em style="margin-left: -10px;">${data.price}</em>
                                            <del>
                                                <em class="yuan">￥</em>${data.originprice}
                                            </del>
                                        </span>
                                    </dd>
                                    <dt>销量：</dt>
                                    <dd style="color: #666666;font-size:14px;line-height: 36px;">
                                        <span style="color: #f77c04">${data.pop}</span>
                                        人已购买
                                    </dd>
                                    <dt>数量：</dt>
                                    <dd id="num">
                                        <span class="reduce btn">-</span>
                                        <input type="text" value="1" class="goods-num">
                                        <span class="add btn">+</span>                                       
                                        <span class="stock">库存&nbsp;<span>${data.num}</span>&nbsp;件</span>
                                    </dd>
                                    <dt>物流：</dt>
                                    <dd><span class="det">由&nbsp;<b>${data.logistics}</b>&nbsp;发货并提供售后服务
                                        <span style="color: red;">&nbsp;免邮费&nbsp;</span></span>
                                    </dd>
                                </dl>
                            </div>
                        </div>
                        <div id="button" class="col-xs-12" style="padding: 0px 0 25px 0;display:block;">
                            <span class="col-xs-6" style="padding: 0;">
                                <a href="" id="btn1"><s class="glyphicon glyphicon-yen"></s>
                                立刻购买</button></a>
                            </span>
                            <span class="col-xs-6" style="padding: 0;">
                                <a href="javascript:void(0);" id="btn2" style="float: right;"><s class="glyphicon glyphicon-shopping-cart"></s>
                                加入购物车</a>
                            </span>
                        </div>
                    `;
                    // console.log(temp3);
                    $('#temp3').append(temp3);
                    $("#temp3").find("#btn1").on("mouseenter",function(ev){
                        var btn=ev.target;
                        if(phone==id){
                            btn.href="./登录.html";
                            console.log(phone,id);
                        } else {
                            btn.href="./购物车.html?phone="+phone;
                        }
                    }),
                    $("#temp3").find("#btn2").on("mouseenter",function(ev){
                        var btn=ev.target;
                        if(phone==id){
                            btn.href="./登录.html";
                        }
                    })
                    
                    var num=$(".goods-num").val();
                    console.log(num);
                    $(".goods-num").on("change keyup",function(){
                        var num=$(".goods-num").val();
                        if(isNaN(num)||num<=0){
                            num=1;
                            $(".goods-num").val(num);
                        }
                        $(".reduce").on('click',function(){
                            num=num-1;
                            if(num<=0){
                                num=1
                                $(".goods-num").val(num);
                            }
                            console.log(num);
                            $(".goods-num").val(num);
                        }),
                        $(".add").on('click',function(){
                            num++; 
                            console.log(num);
                            $(".goods-num").val(num);
                        })
                    }),
                    $(".reduce").on('click',function(){
                        num=num-1;
                        if(num<=0){num=1}
                        console.log(num);
                        $(".goods-num").val(num);
                    }),
                    $(".add").on('click',function(){
                        num++; 
                        console.log(num);
                        $(".goods-num").val(num);
                    })                                       
                    let temp4="";
                    pic1.forEach((element,i) => {
                        temp4+=`
                            <li>
                                <a href="#">
                                    <img src="${pic1[i].src}" alt="">
                                </a>
                                <h5 class="name">
                                    <a href="#">${pic1[i].title}</a>
                                </h5>
                                <p class="price">
                                    <span class="left-price">
                                        <em style="font-style: normal;">￥</em>
                                        ${pic1[i].price}</span>
                                    <span class="right">已有<span>${pic1[i].sale}</span>人购买</span>
                                </p>
                            </li>
                        `;
                    });
                    // console.log(temp4);
                    $('.temp4').append(temp4);
                    let temp5=`
                        <a href="#eva">用户评价(<span>${data.ecanum}</span>)</a>
                    `;
                    // console.log(temp5);
                    $('.temp5').append(temp5);
                    let temp6="";
                    pic2.forEach((element,i) => {
                        temp6+=`
                            <img src="${pic2[i].src}" alt="">
                        `;
                    });
                    // console.log(temp6);
                    $('.temp6').append(temp6);
                    let temp7="";
                    pic3.forEach((element,i) => {
                        temp7+=`
                            <li class="col-xs-12">
                                <div class="item">
                                <img src="${pic3[i].src}" alt="" height="50">
                                <h4 style="font-size: 17px;">${pic3[i].title}
                                    <small>${pic3[i].date}</small><br>
                                    <span>${pic3[i].content}</span>                                                      
                                </h4>
                                </div>
                            </li>
                        `;
                    });
                    // console.log(temp7);
                    $('.temp7').append(temp7);
                    $('body').find('#btn2').on('click', function() {
                        addItem(data.id, data.price, $(".goods-num").val());
                    });
                }
            })
            var length=0;
            var len=document.getElementById("length");
            function addItem(id, price, num) {
                console.log(id,price,num);
                var shop = cookie.get('shop'); //获取cookie
                var product = {
                    id: id,
                    price: price,
                    num: num
                };
                if (shop) {
                    shop = JSON.parse(shop);
                    if (shop.some(elm => elm.id == id)) {
                        shop.forEach(elm => {
                            elm.id == id ? elm.num = num : null;
                        });
                    } else {
                        shop.push(product);
                    }
                } else {
                    shop = [];
                    shop.push(product);
                    console.log(product);
                }
                cookie.set('shop', JSON.stringify(shop), 1);
                shop.forEach((element,i)=>{
                    length++;
                })
                console.log(length);
            }
            console.log(length);
            len.innerHTML=length;
            $("#logo-img").on("mouseenter",function(){
                var logo=document.getElementById("logo-img");
                console.log(logo.href);
                if(phone==id){
                    logo.href="./首页.html";
                    console.log(logo.href);
                } else {
                    logo.href="./首页1.html?phone="+phone;
                }
            })
            var pho=document.getElementById("phone");
            var exit=document.getElementById("exit");
            if(phone==id){
                pho.href="./登录.html";
                exit.hrf="./注册.html"
            } else {
                pho.href="javascript:void(0);";
                exit.hrf="./首页.html"
                pho.innerHTML="[ "+phone+" ]";
                exit.innerHTML="[ 退出 ]";
                $("#phone").css("color","#2d8701");
            }
            
                        
        })
    </script>



    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->
    </head>
    <body>
        <!-- 头部 -->
        <div id="site-nav">
            <div class="container">
                <p class="col-xs-3">嗨~，欢迎来到M宝商城！</p>
                <div class="col-xs-9">
                    <ul id="right">
                        <li><a href="./登录.html" id="phone">登录</a></li>
                        <li><a href="./注册.html" id="exit">注册</a></li>
                        <li><a href="#">我的订单</a></li>
                        <li class="user">
                            <a href="#">个人中心
                                <i class="sj"></i>
                            </a>
                            <ul class="subnav">
                                <li><a href="">我的优惠券</a></li>
                                <li><a href="">我的收藏</a></li>
                                <li><a href="">我的店铺</a></li>
                                <li><a href="">我的邀请</a></li>
                            </ul>
                        </li>
                        <li><a href="#">手机版</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <!-- 顶部logo -->
        <nav class="navbar" style="height: 112px;border-bottom: 1px #eeeeee solid;">
            <div class="container" style="height: 100%;">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header col-xs-4 nav" style="height: 100%;">
                    <a class="navbar-brand" href="" id="logo-img"><img src="http://mbaoshop.oss-cn-shenzhen.aliyuncs.com//Uploads/Picture/2017-07-10/59632c0977fde.png" alt=""></a>
                </div>
                <div class="col-xs-8 nav" style="height: 100%; float: left;">
                    <ul style="padding: 0px;">
                        <li class="col-xs-9" style="padding: 0px;">
                            <form class="navbar-left col-xs-12" style="padding-right:20px;">
                                <div class="form-group col-xs-10" style="padding: 0px;">
                                    <input type="text" class="form-control" placeholder="购买您想要的商品">
                                </div>
                                <button type="submit" class="col-xs-2" >搜索</button>
                            </form>
                        </li>
                        <li class="box col-xs-3" style="padding: 0px;">
                            <a href="#" class="btn btn-positive">
                                <i class="glyphicon glyphicon-shopping-cart"></i>我的购物车
                                <span class="badge" id="length">0</span>
                            </a>
                        </li>
                    </ul>
                </div>
            </div><!-- /.container-fluid -->
        </nav>
        <!-- 导航条 -->
        <div class="navbar" id="navbar">
            <div class="container">
                <div class="navbar-header col-xs-2" id="box">
                    <a href="">
                        浏览全部商品
                        <i class="glyphicon glyphicon-menu-down" style="color: #bbbbbb;"></i>
                    </a>
                </div>
                <div class="after-sale" id="box" style="width: 10%;"><a href="">售后服务</a></div>
                <span style="float: right;line-height: 50px;">客服热线：</span>
            </div>
        </div>
        <div id="product" style="background-color: #F3f3f3">
            <div class="container">
                <div class="temp1">
                </div>
                <div class="col-xs-12" id="Carousel" >
                    <div class="col-xs-6" style="padding-left: 0px;">    
                        <div style="width: 100%;">
                            <img src="" alt="" id="pic" style="width: inherit;padding: 0;margin: 0;">
                        </div>
                        <div class="hd">
                            <ul class="temp2">
                            </ul>
                        </div>
                        <a class="prev" ><i class="icon glyphicon glyphicon-menu-left"></i></a>
                        <a class="next"><i class="icon glyphicon glyphicon-menu-right"></i></a>          
                        <script>
                            $("#product #Carousel .hd ul li").eq(0).val("1");
                            $(function(){
                                var img=document.querySelector('#pic'); 
                                $("#product #Carousel .next").on('click',function(){
                                    var i=$("#product #Carousel .hd ul li[value='1']").index();
                                    if(i==3){ i=-1;}
                                    $("#product #Carousel .hd ul li").val("0");
                                    $("#product #Carousel .hd ul li").eq(i+1).val("1");
                                    $("#product #Carousel .hd ul li").css('border','0');
                                    $("#product #Carousel .hd ul li[value='1']").css('border','2px solid #2d8701');
                                    var attr=$('#product #Carousel .hd ul li[value="1"] img').attr("src");
                                    img.src=attr;
               
                                    console.log(attr);
                                }),
                                $("#product #Carousel .prev").on('click',function(){
                                    var i=$("#product #Carousel .hd ul li[value='1']").index();
                                    if(i==3){ i=-1;}
                                    $("#product #Carousel .hd ul li").val("0");
                                    $("#product #Carousel .hd ul li").eq(i-1).val("1");
                                    $("#product #Carousel .hd ul li").css('border','0');
                                    $("#product #Carousel .hd ul li[value='1']").css('border','2px solid #2d8701');
                                    var attr=$('#product #Carousel .hd ul li[value="1"] img').attr("src");
                                    img.src=attr;
               
                                    console.log(attr);
                                })
                            })
                        </script>
                    </div>
                    <div class="col-xs-6"> 
                        <div id="temp3"></div>
                        <div class="weChat" style="display:block;">
                            <img src="../img/wechat.png" width="80" height="80" alt="" >
                            <h4 style="display: inline-block;"><i></i>微信采购</h4>
                            <p>一键购买，快捷无忧！</p>
                        </div>
                        <div class="share">
                            <div style="padding-top: 15px;">
                                <span style="color: #aaaaaa">分享到：</span>
                                <span class="pic"><a href="#"></a></span>
                                <span class="pic" style="background-position: -40px 0px;"><a href="#"></a></span>
                                <span class="pic" style="background-position: -80px 0px ;"><a href="#"></a></span>
                                <span style="margin-left:240px;">
                                    <a href="#" >
                                        <i class="glyphicon glyphicon-heart-empty"></i>
                                        加入收藏
                                    </a>
                                </span>
                            </div>
                        </div>
                    </div>    
                </div>
                <!-- 推荐评价 -->
                <div id="user-det">
                    <div class="col-xs-3" id="left">
                        <h5 class="title">看过本商品的用户还购买过</h5>
                        <ul class="temp4">
                        </ul>
                    </div>
                    <div class="col-xs-9" id="right" style="padding: 0;">
                        <div class="Tab col-xs-12">
                            <ul>
                                <li><a href="#intro">商品介绍</a></li>
                                <li class="temp5"></li>
                            </ul>
                        </div>
                        <div class="title col-xs-12">
                            <h3><a href="#" name="intro">商品详情</a></h3>
                            <div class="temp6">
                            </div>
                            <h3><a href="#" name="eva">用户评价</a></h3>
                        </div>
                        <div class="content col-xs-12">
                            <ul class="temp7">
                            </ul>
                        </div>
                    </div>
                </div>
            </div> 
        </div>
        <!-- 底部 -->
        <footer id="footer" style="background: #444;">
                <div class="container">
                    <div id="map">
                        <ul class="col-xs-12">
                            <li class="col-xs-2">
                                <a href="">
                                    <i><img src="../img/map1.png" alt=""></i>
                                    <h4>帮助中心</h4>
                                </a>
                                <p>积分规则 邀请规则</p>
                            </li>
                            <li class="col-xs-2">
                                <a href="">
                                    <i><img src="../img/map2.png" alt=""></i>
                                    <h4>购物指南</h4>
                                </a>
                                <p>免费注册 快速下单</p>
                            </li>
                            <li class="col-xs-2">
                                <a href="">
                                    <i><img src="../img/map3.png" alt=""></i>
                                    <h4>售后服务</h4>
                                </a>
                                <p>售后政策 取消订单</p>
                            </li>
                            <li class="col-xs-2">
                                <a href="">
                                    <i><img src="../img/map4.png" alt=""></i>
                                    <h4>正品保障</h4>
                                </a>
                                <p>正品行货 放心选购</p>
                            </li>
                        </ul>
                    </div>
                    <div id="info">
                        <a href="">关于我们</a><em>|</em>
                        <a href="">购物指南</a><em>|</em>
                        <a href="">售后服务</a>
                    </div>
                    <div id="copyright">
                        <p>©  M宝商城  凯盛嘉业网络科技有限公司   粤ICP备17071557号-1   增值电信业务经营许可证：粤B2-20170758  食品经营许可证：JY14401060269164(1-1)</p>
                    </div>
                    
                </div>
            </footer>
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
  </body>
</html>